<?php
/**
 * quick_curd
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2025/3/6
 * 描述:
 */

namespace app\service;

class AliyunService
{
    //快递查询
    public static function kd($express_no, $type = '', $order_sn = '')
    {
        $url = "https://wuliu.market.alicloudapi.com/kdi";//api访问链接
        $config = config('aliyun.market');
        $appcode = $config['app_code'];
        if (!$appcode) {
            return data_return_arr('请配置阿里云市场接口信息', 1);
        }
        $headers = [
            "Authorization:APPCODE " . $appcode
        ];
        $param = [
            'no' => trim($express_no),
            'type' => trim($type),
        ];
        $res = bs_curl_get($url, $param, 10, $headers);
        if ($res === false) {
            return data_return_arr('快递查询失败', 1);
        }
        bs_log($res, '快递查询', 'kd.log');

        $res = json_decode($res, true);
        if ($res['status'] != 0) {
            $statusArr = [
                201 => '快递单号错误',
                203 => '快递公司不存在',
                204 => '快递公司识别失败',
                205 => '没有信息',
                207 => '该单号被限制，错误单号',
            ];
            return data_return_arr($statusArr[$res['status']] ?? $res['msg'], 1);
        }
        $result = $res['result'];
        $list = $result['list'];
        $expName = $result['expName'] ?? '';

        if ($list) {
            //time和content 字段
            foreach ($list as &$v) {
                $v['content'] = $v['status'];
            }
            unset($v);
        }
        return data_return_arr('success', 0, [
            'express_no' => $express_no,
            'express_name' => $expName,
            'list' => $list,
            'type' => $result['type'] ?? $type,
        ]);
    }
}
